/**
 * 展示表格ACTION
 */
package cn.antia.cwin.page;

import java.util.*;
import javax.servlet.http.*;

import cn.antia.cwin.grid.GridService;
import cn.antia.cep.common.*;
import cn.antia.cwin.page.StrutsAction;
import cn.antia.cep.page.action.ActionException;
import cn.antia.cwin.grid.AntiaNormalTable;
import cn.antia.cwin.Cele;

/**
 * @author sunkeei
 * @date Nov 21, 2007
 * @version 0.11
 * @time 
 */
public class GridAction extends StrutsAction {
	private String action;
	
	private GridService gridService;
	public void setGridService(GridService gridService) {
		this.gridService = gridService;
	}

	protected String action() throws AntiaException {
		if(action == null || action.trim().length() < 1){
			action = "go";
		}
		if(action.equals("go")){
			return singleGrid();
		}else if(action.equals("query")){
			return queryGrid();
		}else if(action.equals("goPage")){
			return goPage();
		}
		request.setAttribute("err_msg", "unknow action:" + action);
		AntiaLog.error("GridAction unknow action:" + action);
		return "error";
	}
	/**
	 * 取得singleGrid 以及数据
	 * @return
	 */
	private String singleGrid(){
		String id = request.getParameter("id");
		AntiaNormalTable table = gridService.getSingleAntiaNormalTable(id);
		request.setAttribute("dataTable", table);
		setSessionQueryCache(id,null);
		return "single";
	}
	/**
	 * 查询singleGrid
	 * @return
	 */
	private String queryGrid(){
		String id = request.getParameter("id");
		Map<Cele,Object> limits = getFieldValues(request);
		AntiaNormalTable table = gridService.getSingleAntiaNormalTable(id,limits);
		request.setAttribute("dataTable", table);
		setSessionQueryCache(id,limits);
		return "single";
	}
	/**
	 * 分页的跳转
	 * @return
	 */
	private String goPage(){
		String id = request.getParameter("id");
		String topage = request.getParameter("topage");
		int page = Integer.parseInt(topage);
		Map<Cele,Object> limits = getQueryCache(id);
		AntiaNormalTable table;
		if(limits == null || limits.size() == 0){
			table = gridService.getSingleAntiaNormalTable(id,page);
		}else{
			table = gridService.getSingleAntiaNormalTable(id,limits,page);
		}
		request.setAttribute("dataTable", table);
		return "single";
	}
	
	 /**
     * 取出参数构造List对象返回
     * @param request request
     * @return  List
     */
    protected Map<Cele,Object> getFieldValues(HttpServletRequest request) throws ActionException {
        Map<Cele,Object> elements = new HashMap<Cele,Object>();
        Cele ec;
        String paraName;
        String paraValue;
        Map map = request.getParameterMap();
        Iterator pamas = map.keySet().iterator();
        while (pamas.hasNext()) {
        	ec = new Cele();
            paraName = (String) pamas.next();
            paraValue = request.getParameter(paraName).trim();
            if (paraName.equalsIgnoreCase("id") || paraName.equalsIgnoreCase("action") || paraName.equalsIgnoreCase("errorMsgSpan")) {
                continue;
            }
            if(paraValue != null && paraValue.trim().length() > 0){
            	ec.getField().setFieldName(paraName);
            	elements.put(ec,paraValue);
            }
        }
        return elements;
    }
    /**
     * 将Grid的查询状态保存在Session中，以便翻页时调用这个查询状态
     * @param id	ID
     * @param limits	查询条件
     */
    private void setSessionQueryCache(String id,Map<Cele,Object> limits){
    	HashMap map = (HashMap)session.getAttribute(SessionConst.GRID_QUERY_CACHE);
    	if(map == null){
    		map = new HashMap();
    		session.setAttribute(SessionConst.GRID_QUERY_CACHE, map);
    	}
    	map.put(id, limits);
    }
    /**
     * 取得保存在Session中的查询状态
     * @param id
     * @return
     */
    private Map<Cele,Object> getQueryCache(String id){
    	HashMap map = (HashMap)session.getAttribute(SessionConst.GRID_QUERY_CACHE);
    	return (Map<Cele,Object>)map.get(id);
    }

	public void setAction(String action) {
		this.action = action;
	}
}
